package com.atguigu.gmall.cart.controller;

import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.cart.CartInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping(value = "/cart")
public class CartController {
    @Autowired
    private CartService cartService;

    @Autowired
    private HttpServletRequest request;

    /**
     * 新增购物车
     * @param skuId
     * @param num
     * @return
     */
    @GetMapping(value = "/add")
    public Result addCart(Long skuId, Integer num){
        //获取用户id
        String userId = "";
//        String tempUserId = request.getHeader("tempUserId");
        String tempUserId = "562d46c73b52497d8974c6d2673791ae";
        if (!StringUtils.isEmpty(userId)){
            //登陆用户的新增
            cartService.addCart(skuId,num,userId);
            return Result.ok();
        }else {
            //临时用户的新增
            String cart = cartService.addCartTemp(skuId,num,tempUserId);
            return Result.ok(cart);
        }
    }


    /**
     * 查询购物车
     * @return
     */
    @GetMapping(value = "/getCart")
    public Result getCart(){
        //测试一,都为空--返回空
//        String userId = "";
//        String tempUserId = "";
        //测试二,返回登录用户的购物车数据即可
//        String userId = "2";
//        String tempUserId = "";
        //测试三, 返回临时用户的购物车信息即可
//        String userId = "";
//        String tempUserId = "562d46c73b52497d8974c6d2673791ae";
        String userId = "1";
        String tempUserId = "562d46c73b52497d8974c6d2673791ae";
        List<CartInfo> cartInfoList = cartService.getCartInfoList(userId,tempUserId);
        return Result.ok(cartInfoList);
    }

    /**
     * 修改购物车数据
     * @param skuId
     * @param num
     * @return
     */
    @PutMapping(value = "/updateCart")
    public Result updateCart(Long skuId, Integer num){
            String userId = "1";
            cartService.updateCartInfo(skuId,num,userId);
            return Result.ok();
    }

    /**
     * 查询购物车信息
     * @param userId
     * @return
     */
    @GetMapping(value = "/getChooseCartInfoList/{userId}")
    public List<CartInfo> getChooseCartInfoList(@PathVariable(value = "userId")Long userId){
        return cartService.getChooseCartInfoList(userId);

    }

    /**
     * 删除购物车
     * @param userId
     * @param skuId
     * @return
     */
    @GetMapping(value = "/delCart/{userId}/{skuId}")
    public Result delCart(@PathVariable(value = "userId") Long userId, @PathVariable(value = "skuId") Long skuId){
        cartService.deleteCart(userId,skuId);
        return Result.ok();
    }


}
